package com.ymatou.iapi.payment.testcase.old;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.payment.parameter.*;
import com.ymatou.iapi.payment.service.*;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.PaymentProxyCallService;
import com.ymttest.database.model.PPBussinessOrder;
import com.ymttest.database.model.PPPayment;
import com.ymttest.database.model.RefundRequest;
import com.ymttest.database.sqlwapper.PaymentDefaultWapper;
import com.ymttest.utils.YMTDateUtil;

/**
 * 系统自动审核退款 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SysApproveRefund {
	private static SysApproveRefundBean sysapproverefundBean;
	private static SysApproveRefundCall sysapproverefundCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("系统自动审核退款");
	}

	@Before
	public void caseUp() {
		sysapproverefundBean = new SysApproveRefundBean();
		sysapproverefundCall = new SysApproveRefundCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sysapproverefundCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SysApproveRefund_001() {
		Logger.start(true, "调用自动审核成功");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("10");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			PaymentProxyCallService.submitRefund(aqb);
			sysapproverefundBean.setBizId(YMTDateUtil.getBeforeOrNextHour(1, "yyyy-MM-dd HH:00:00"));
			sysapproverefundCall.setData(sysapproverefundBean);
			sysapproverefundCall.callService();
			Logger.verifyEquals("ok", sysapproverefundCall.getReturnData(), "验证返回");
			Thread.sleep(5000);
			verifyDB(aqb);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SysApproveRefund_002() {
		Logger.start(true, "不输入BizId,失败");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("10");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			PaymentProxyCallService.submitRefund(aqb);
//			sysapproverefundBean.setBizId(YMTDateUtil.getBeforeOrNextHour(1, "yyyy-MM-dd HH:00:00"));
			sysapproverefundCall.setData(sysapproverefundBean);
			sysapproverefundCall.callService();
			Logger.verifyEquals("Date and Patterns must not be null", sysapproverefundCall.getReturnData(), "验证返回");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SysApproveRefund_003() {
		Logger.start(true, "输入错误格式BizId,失败");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("10");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			PaymentProxyCallService.submitRefund(aqb);
			sysapproverefundBean.setBizId(YMTDateUtil.getBeforeOrNextHour(1, "yyyyMMddHHmmss"));
			sysapproverefundCall.setData(sysapproverefundBean);
			sysapproverefundCall.callService();
			Logger.verifyIsContains("请求处理失败|system approve refund receive invalid schedule time", sysapproverefundCall.getReturnData(), "验证返回");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	@Ignore
	//TODO 暂需手工验证
	public void Tc_SysApproveRefund_004() {
		Logger.start(true, "时间范围内退款数超出设定值,失败");
		try {
			AcquireOrderBean aqb = PaymentProxyCallService.initAcquireOrder("10");
			PaymentProxyCallService.acquireOrder(aqb);
			PaymentProxyCallService.notifyCall(aqb);
			PaymentProxyCallService.submitRefund(aqb);
			AcquireOrderBean aqb2 = PaymentProxyCallService.initAcquireOrder("10");
			PaymentProxyCallService.acquireOrder(aqb2);
			PaymentProxyCallService.notifyCall(aqb2);
			PaymentProxyCallService.submitRefund(aqb2);
			
			sysapproverefundBean.setBizId(YMTDateUtil.getBeforeOrNextHour(1, "yyyy-MM-dd HH:00:00"));
			sysapproverefundCall.setData(sysapproverefundBean);
			sysapproverefundCall.callService();
			Logger.verifyIsContains("请求处理失败", sysapproverefundCall.getReturnData(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	 private void verifyDB(AcquireOrderBean aqb) throws Exception {
	        Logger.comment("验证数据库RefundRequest");
	        PaymentDefaultWapper paymentwapper = new PaymentDefaultWapper();
	        PPBussinessOrder ppb = paymentwapper.selectPPBussinessorderByTraceId(aqb.getTraceId()).get(0);
	        String businessoid = ppb.getBussinessOrderId();
	        PPPayment ppm = paymentwapper.selectPPPaymentByBussinessOrderId(businessoid).get(0);
	        String trandno = ppm.getPaymentId();
	        List<RefundRequest> mlist = paymentwapper.selectRefundRequestByPaymentId(trandno);
	        Logger.debug(mlist);
	        for (RefundRequest map : mlist) {
	        	Logger.verifyEquals(1, map.getApproveStatus(), "验证ApproveStatus");
	        	Logger.verifyEquals("system", map.getApprovedUser(), "验证ApprovedUser");
			}
	        
	 }
}